<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <h2 style="text-align:center">登录</h2>
      <el-form :model="form" :rules="rules" ref="loginForm" label-width="0" class="login-form">
        <el-form-item prop="username">
          <el-input id="acct" v-model="form.username" placeholder="用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input id="pwd" v-model="form.password" placeholder="密码" show-password />
        </el-form-item>

        <el-form-item>
          <el-button id="submit" type="primary" style="width:100%" @click="submitLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loginForm = ref<{ validate?: (cb: (valid: boolean) => void) => void } | null>(null)
const form = ref({ username: '', password: '' })

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const submitLogin = () => {
  loginForm.value?.validate?.((valid: boolean) => {
    if (!valid) return
    if (form.value.username === 'admin' && form.value.password === 'admin123') {
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error('账号或密码错误')
    }
  })
}
</script>

<style scoped>
.login-container {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding-top: 80px;
}

.login-card {
  width: 360px;
  padding: 20px;
}

.login-form {
  margin-top: 10px;
}
</style>
